package com.dongdong.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;

import com.dongdong.UserToken.UserEnums;
import com.dongdong.UserToken.UserToKen;
import com.dongdong.api.SellerServiceApi;
import com.dongdong.model.TAdmin;
import com.dongdong.model.TbSeller;
import com.dongdong.result.Result;
import com.dongdong.result.ResultEnums;
import com.dongdong.utils.JwtUtil;
import com.dongdong.vo.TAdminVO;
import com.dongdong.vo.TbSellerVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.dongdong.utils.ShiroUtils.encryptPassword;

@RestController
@RequestMapping("login")
public class LoginController {

    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private SellerServiceApi sellerServiceApi;

    @PostMapping("/adminlogin")
    public Result adminlogin(@RequestParam String username,@RequestParam String password) {


        if (StrUtil.isEmpty(username) || StrUtil.isEmpty(password)) {
            return  Result.RESPONSE(ResultEnums.USERNAME_AND_PASSWORD_NOT_NULL); //5：操作失败
        }


        //1. 获取subject
        Subject subject = SecurityUtils.getSubject();
        //2.封装Token
//        UsernamePasswordToken usernamePasswordToken= new UsernamePasswordToken(username, password);
        UserToKen userToKen= new UserToKen(username, password, UserEnums.ADMIN);
        //3.调用Login方法
        try {
            subject.login(userToKen);
        }catch (UnknownAccountException e){
            e.printStackTrace();
            return  Result.RESPONSE(ResultEnums.USERNAME_AND_PASSWORD_ERROR);
        }catch(IncorrectCredentialsException e){
            return  Result.RESPONSE(ResultEnums.USERNAME_AND_PASSWORD_ERROR);
        }


        //获取user
        TAdmin tAdmin = (TAdmin) SecurityUtils.getSubject().getPrincipal();
        //在生成token是需要做脱敏处理
        TAdminVO tAdminVO = new TAdminVO();
        BeanUtils.copyProperties(tAdmin, tAdminVO);

        // 生成token
        String token = jwtUtil.createJwt(tAdminVO.getId().toString(), JSONObject.toJSONString(tAdminVO), "adminRoles");

/*//         解析token   测试
         Claims claims = jwtUtil.parseJwt(token);
         String id = claims.getId();
         System.out.println("id------>>"+id);
         String userStr = claims.getSubject();
//         转换成对象  脱敏处理
        TAdminVO user1 = JSONObject.parseObject(userStr, TAdminVO.class);
         System.out.println(user1);*/

        return  Result.RESPONSE(ResultEnums.LOGIN_SUCCESS).setData(token);//登录成功

    }

    @PostMapping("/sellerlogin")
    public Result sellerlogin(@RequestParam String username,@RequestParam String password) {

        if (StrUtil.isEmpty(username) || StrUtil.isEmpty(password)) {
            return  Result.RESPONSE(ResultEnums.USERNAME_AND_PASSWORD_NOT_NULL); //5：操作失败
        }

        //1. 获取subject
        Subject subject = SecurityUtils.getSubject();
        //2.封装Token
//        UsernamePasswordToken usernamePasswordToken= new UsernamePasswordToken(username, password);
        UserToKen userToKen= new UserToKen(username, password, UserEnums.SELLER);
        //3.调用Login方法
        try {
            subject.login(userToKen);
        }catch (UnknownAccountException e){
            e.printStackTrace();
            return  Result.RESPONSE(ResultEnums.USERNAME_AND_PASSWORD_ERROR);
        }catch(IncorrectCredentialsException e){
            return  Result.RESPONSE(ResultEnums.USERNAME_AND_PASSWORD_ERROR);
        }


        //获取user
        TbSeller tbSeller = (TbSeller) SecurityUtils.getSubject().getPrincipal();
        //在生成token是需要做脱敏处理
        TbSellerVO tbSellerVO = new TbSellerVO();
        BeanUtils.copyProperties(tbSeller, tbSellerVO);

        // 生成token
        String token = jwtUtil.createJwt(tbSellerVO.getSellerId().toString(), JSONObject.toJSONString(tbSellerVO), "sellerRoles");

/*//         解析token   测试
         Claims claims = jwtUtil.parseJwt(token);
         String id = claims.getId();
         System.out.println("id------>>"+id);
         String userStr = claims.getSubject();
//         转换成对象  脱敏处理
        TAdminVO user1 = JSONObject.parseObject(userStr, TAdminVO.class);
         System.out.println(user1);*/

        return  Result.RESPONSE(ResultEnums.LOGIN_SUCCESS).setData(token);//登录成功


    }


    @GetMapping("/unameseller")  //注册时保障账号唯一性
    public int   unameseller (@RequestParam String uname){
        return sellerServiceApi.unameseller(uname);
    }
    //注册

    @PostMapping("/sellerregiest")
    public Result sellerregiest(@RequestBody TbSellerVO sellerVO){
//        tbSeller.setPassword(encryptPassword("md5", tbSeller.getPassword(), tbSeller.getSalt(),5));//密5次

        return sellerServiceApi.sellerregiestadd(sellerVO);
    }



















}
